﻿using Furion.DataEncryption;
using Furion.FriendlyException;
using ZeroAdmin.Application.Entity;
using ZeroAdmin.Core;

namespace ZeroAdmin.Application;

/// <summary>
/// 用户服务
/// </summary>
public class UserService : IUserService
{
    /// <summary>
    /// 用户信息列表查询
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<PageListBase<QueryUserOutput>> GetUserPageList(QueryUserInput input)
    {
        return await SqlSugarHelper.Queryable<User>().LeftJoin<Role>((u, r) => u.RoleId.Equals(r.Id))
            .WhereIF(!string.IsNullOrEmpty(input.UserName), (u, r) => u.UserName.Contains(input.UserName))
            .WhereIF(!string.IsNullOrEmpty(input.PhoneNumber), (u, r) => u.PhoneNumber.Contains(input.PhoneNumber))
            .WhereIF(input.RoleId != 0, (u, r) => u.RoleId.Equals(input.RoleId))
            .OrderByDescending((u, r) => u.Id)
            .Select<QueryUserOutput>((u, r) => new QueryUserOutput
            {
                UserId = u.Id,
                UserName = u.UserName,
                PhoneNumber = u.PhoneNumber,
                LastLoginTime = u.LastLoginTime,
                RoleId = r.Id,
                RoleName = r.RoleName,
                RolePermission = r.RolePermission,
            }).MyToPageListAsync(input.PageIndex, input.PageSize);
    }

    /// <summary>
    /// 添加用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<long> AddUser(AddUserInput input)
    {
        if (await SqlSugarHelper.Queryable<User>().AnyAsync(x => x.PhoneNumber.Equals(input.PhoneNumber)))
        {
            throw Oops.Bah($"该手机号{input.PhoneNumber}已被注册");
        }
        User user = new User();
        user.PhoneNumber = input.PhoneNumber;
        user.UserName = input.UserName;
        user.Password = MD5Encryption.Encrypt(input.Password);
        user.RoleId = input.RoleId;

        return await SqlSugarHelper.InsertReturnIdentityAsync(user);
    }

    /// <summary>
    /// 更新用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<long> UpdateUser(UpdateUserInput input)
    {
        var user = await SqlSugarHelper.Queryable<User>().FirstAsync(x => x.Id.Equals(input.UserId));
        if (user == null)
        {
            throw Oops.Bah($"该用户不存在。");
        }
        if (await SqlSugarHelper.Queryable<User>().AnyAsync(x => x.PhoneNumber.Equals(input.PhoneNumber) && !x.Id.Equals(input.UserId)))
        {
            throw Oops.Bah($"该手机号{input.PhoneNumber}已被注册");
        }
        user.PhoneNumber = input.PhoneNumber;
        user.UserName = input.UserName;
        user.Password = string.IsNullOrEmpty(input.Password) ? user.Password : MD5Encryption.Encrypt(input.Password);
        user.RoleId = input.RoleId;

        await SqlSugarHelper.UpdateAsync(user);
        return user.Id;
    }

    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="UserId"></param>
    /// <returns></returns>
    public async Task DeleteUser(long UserId)
    {
        var user = await SqlSugarHelper.Queryable<User>().FirstAsync(x => x.Id.Equals(UserId));
        if (user == null)
        {
            throw Oops.Bah($"该用户不存在。");
        }

        await SqlSugarHelper.DeleteAsync(user);
    }
}
